import java.util.*;

/**
 * @author gao
 * @date 2025/6/29 22:39
 */

class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;

    public TreeNode() {
    }

    public TreeNode(int val) {
        this.val = val;
    }

    public TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }
}

public class Solution {
    public List<Integer> preorderTraversal(TreeNode root) {
        List<Integer> tree = new ArrayList<>();
        PreOrder(root, tree);
        return tree;
    }

    public void PreOrder(TreeNode root, List<Integer> tree) {
        if (root == null) {
            return;
        }
        tree.add(root.val);
        PreOrder(root.left, tree);
        PreOrder(root.right, tree);
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNext()) {
            int n = in.nextInt();
            int[] nums = new int[n];
            for (int i = 0; i < n; i++) {
                nums[i] = in.nextInt();
            }
            TreeNode root = BuildTree(nums);
            Solution solution = new Solution();
            System.out.println(solution.preorderTraversal(root));
        }
    }

    private static TreeNode BuildTree(int[] nums) {
        if (nums == null && nums.length == 0) {
            return null;
        }

        Queue<TreeNode> queue = new LinkedList<>();
        TreeNode root = new TreeNode(nums[0]);
        queue.offer(root);

        int i = 1;

        while (!queue.isEmpty() && i < nums.length) {
            TreeNode curNode = queue.poll();

            if (i < nums.length && nums[i] != -1) {
                curNode.left = new TreeNode(nums[i]);
                queue.offer(curNode.left);
            }
            i++;

            if (i < nums.length && nums[i] != -1) {
                curNode.right = new TreeNode(nums[i]);
                queue.offer(curNode.right);
            }
            i++;
        }
        return root;
    }
}